package com.s69.ssm.service;

import com.s69.ssm.entity.QueryParams;
import com.s69.ssm.entity.Student;
import com.s69.ssm.mapper.StudentMapper;
import com.s69.ssm.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private SkillService skillService;

    public List<Student> list(QueryParams queryParams) {
        return studentMapper.list(queryParams);
    }

    public Student get(Integer id) {
        return studentMapper.get(id);
    }

    @Transactional
    public void update(Student student) {
        // 删除原头像
        FileUtil.delFile(studentMapper.get(student.getId()).getFaceimg());
        // 删除学生所有技能
        skillService.delByStuId(student.getId());
        if(student.getSkills() != null && student.getSkills().size()>0) {
            // 保存学生技能
            skillService.insert(student.getId(), student.getSkills());
        }
        studentMapper.update(student);
    }

    public Student getByStuNo(String stuno) {
        return studentMapper.getByStuNo(stuno);
    }

    @Transactional
    public void del(Integer id) {
        Student student = studentMapper.get(id);
        // 删除身份证
        FileUtil.delFile(student.getIdcardPositive());
        FileUtil.delFile(student.getIdcardNegative());
        // 删除头像
        FileUtil.delFile(student.getFaceimg());
        // 删除学生所有技能
        skillService.delByStuId(id);
        // 删除学生
        studentMapper.del(id);

    }

    @Transactional
    public void add(Student student) {
        studentMapper.add(student);
        if(student.getSkills() != null && student.getSkills().size()>0) {
            // 保存学生技能
            skillService.insert(student.getId(), student.getSkills());
        }
    }

    @Transactional
    public void updateIdCardFile(Student student) {
        Student dbStudent = studentMapper.get(student.getId());
        // 删除身份证
        FileUtil.delFile(dbStudent.getIdcardPositive());
        FileUtil.delFile(dbStudent.getIdcardNegative());
        studentMapper.update(student);
    }
}
